﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LadderScript : MonoBehaviour {
	
	public bool isLadder;
	public float maxY;
	playerScript player;
	bool playerFlag;
	
	public GameObject Tail;
	public GameObject Body;
	int size = 0;
	
	public void setSize(int p_Size)
	{
		if(size == p_Size)
			return;
		size = p_Size;
		if(size < 3)
			size = 3;
		
		Vector3 tmpPos = Vector3.zero;
		tmpPos.y = -size + 1;
		Tail.transform.localPosition = tmpPos;
		
		tmpPos = Vector3.zero;
		tmpPos.y = (-size + 1f) / 2;
		Body.transform.localPosition = tmpPos;
		
		Vector3 tmpScale = Body.transform.localScale;
		tmpScale.y = size - 2;
		Body.transform.localScale = tmpScale;
		
		Body.renderer.material.mainTextureScale = new Vector2(1,size - 2);
		
		BoxCollider tmpCol = GetComponent<BoxCollider>();
		tmpCol.size = new Vector3(0.18f,size - 1.6f,1);
		tmpCol.center = new Vector3(0,((float)(-size + 3) / (float)2),0);
		
	}
	void Awake()
	{
		setSize(3);
	}
	// Use this for initialization
	void Start () {
		
		playerFlag = false;
		if(!isLadder)
			maxY = transform.position.y - 1;
		else
			maxY = float.MaxValue;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnTriggerEnter(Collider myCol)
	{
		if(myCol.tag == "Player" && myCol.networkView.isMine)	
		{
			if(!playerFlag)
			{
				playerFlag = true;
				player = myCol.GetComponent<playerScript>();
			}
			
			player.OnLadderEnter(this);
		}
	}
	
	void OnTriggerStay(Collider myCol)
	{
		if(myCol.tag == "Player" && myCol.networkView.isMine)	
		{
			player.OnLadderEnter(this);
		}
	}
	
	void OnTriggerExit(Collider myCol)
	{
		if(myCol.tag == "Player" && myCol.networkView.isMine)	
		{
			player.OnLadderExit(this);
		}
	}
}
